package com.safe.ly_safe.utils;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.safe.ly_safe.R;
import com.safe.ly_safe.entity.SdpNameBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @PACKAGE :com.safe.ly_safe.utils
 * @Author : HSF
 * @Time : On 2022/4/25 10:12
 * @Description : SdpSelectPopup
 */
public class SdpSelectPopup extends PopupWindow {

    private Activity mContext;

    public boolean canDismiss = true;

    private BaseQuickAdapter mAdapter;

    private List<SdpNameBean> sdpNameList = new ArrayList<>();

    private OnSdpSelectItemListener onSdpSelectItemListener;

    private RecyclerView mRecyclerView;

    public SdpSelectPopup(Activity mContext,List<SdpNameBean> sdpNameList) {
        this.mContext = mContext;
        this.sdpNameList = sdpNameList;
        initPopWindow();
    }

    private void initPopWindow(){
        View view = LayoutInflater.from(mContext).inflate(R.layout.sdp_select_popup, null);
        this.setContentView(view);

        int mWidth = MyUntil.getScreenWidth(mContext) - MyUntil.dp2px(mContext,80);
        this.setWidth(mWidth);
        // 设置弹出窗体的高
        this.setHeight(MyUntil.dp2px(mContext,sdpNameList.size() * 40));
        // 设置弹出窗体可点击
        // this.setFocusable(true);
        //this.setOutsideTouchable(true);
        // 刷新状态
        //this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
//        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);

        //backgroundAlpha(mContext,0.5f);//0.0-1.0
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.pop_anim);
//        this.setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                //backgroundAlpha(mContext,1f);
//                //proSelectPopupMissListener.selectPopupMiss();
//            }
//        });
        mRecyclerView = view.findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new BaseQuickAdapter<SdpNameBean, BaseViewHolder>(R.layout.sdp_item_layout,sdpNameList) {
            @Override
            protected void convert(BaseViewHolder helper, SdpNameBean item) {
                TextView mTv = helper.getView(R.id.nameTv);
                mTv.setText(item.getName());
                ImageView checkIv = helper.getView(R.id.checkIv);
                if(item.isCheck()){
                    checkIv.setVisibility(View.VISIBLE);
                }else{
                    checkIv.setVisibility(View.GONE);
                }
                View lineView = helper.getView(R.id.lineView);
                if(helper.getAdapterPosition() == sdpNameList.size() - 1){
                    lineView.setVisibility(View.GONE);
                }else{
                    lineView.setVisibility(View.VISIBLE);
                }
            }
        };
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < sdpNameList.size(); i++) {
                    sdpNameList.get(i).setCheck(false);
                    if(i == position){
                        sdpNameList.get(i).setCheck(true);
                    }
                }
                mAdapter.notifyDataSetChanged();
                onSdpSelectItemListener.onSdpPopupClick(sdpNameList.get(position).getName());
                dismiss();
            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * canDismiss为false,可以控制dismiss()无参方法不调用, 主要是为了点击PopupWindow外部不消失
     * canDismiss为true之后，即可正常dismiss
     */
    @Override
    public void dismiss() {
        /**
         * 点击外部不消失，点击back键可消失
         */
//        if(canDismiss){
//            dismiss2();
//        } else {
//            StackTraceElement[] stackTrace = new Exception().getStackTrace();
//            if(stackTrace.length >= 2 && "dispatchKeyEvent".equals(stackTrace[1].getMethodName())){
//                dismiss2();
//            }
//        }
        /**
         * 点击外部、back键都不消失
         */
        if(canDismiss){
            dismiss2();
        }
    }

    public void dismiss2(){
        super.dismiss();
    }

    public void showPopWindow(View parent){
        if (!this.isShowing()) {
            this.showAsDropDown(parent);
            //this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            //this.showAtLocation(parent, Gravity.CENTER, 0, 0);
        } else {
            this.dismiss();
        }
    }

    public void setOnSdpClickListener(OnSdpSelectItemListener onSdpSelectItemListener) {
        this.onSdpSelectItemListener = onSdpSelectItemListener;
    }

    public interface OnSdpSelectItemListener{
        void onSdpPopupClick(String selectName);
    }
}
